The term mental gymnastics refers to a great effort that's needed to understand or justify something. For example, some complicated novel plots require mental gymnastics to get a hold of what's happening.
Gymnastics demands a lot from your body in terms of strength, flexibility, coordination, balance, and timing. You often have to move in unusual ways, or adopt and maintain tricky positions. Mental gymnastics demands a lot from your mind: you have to think hard to follow the reasoning, or to keep track of a lot of details and interwoven threads of information. People usually use the phrase negatively in the context of something that is way more complex and difficult to follow than it should be.